﻿using Microsoft.Win32;
using Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Web.Script.Serialization;
using System.Windows;
using Util;
using System.ComponentModel;
using System.Management;

namespace WpfApp1
{
    /// <summary>
    /// Login.xaml 的交互逻辑
    /// </summary>
    public partial class Login : System.Windows.Window
    {
        public Hashtable fileHash = new Hashtable();

        public List<ExamInee> examIneeList = new List<ExamInee>();

        public Group groupData = new Group();

        public List<Job> jobList = new List<Job>();

        private OpenFileDialog openFileDialog = null;

        public String g_text = "小组名称";

        public Login()
        {
            InitializeComponent();

            WindowStartupLocation = WindowStartupLocation.CenterScreen;

            //设置app运行目录
            Config.appPath = Directory.GetCurrentDirectory();

            //设置我的文档目录
            Config.Document = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\";

            try
            {

                var key_string = RSAHelper.PEMToXML_All(Config.public_key, false);

                string content = RSAHelper.EncrytByPublic(key_string, "123123");
            }
            catch (Exception)
            {

            }

            //RSAHelper.DecryptByPrivate(RSAUtils.PEMConvertToXML(), "ZD9OsqlzcOJhU6hjETg2qTKiP5hdfeeEougBqpHMTJXkAv1p1DI6yoMbj3luTkQyBJvwrv2iCzLZhDbpGvxPYH99aBjWebB6DoxJCEPm4Im0vhrr0H9ymAyxPFjOAc8/I8CRISfzmdt1wOcdWYfh3vFgUSyIYjnDODLxr89WDZGHsr2jJU/GO0RXpXFQI2duN1ZexH12QXhCEjkmXoYoC+SJM1o6LOCDxWCJkNk0EW+myzg0McZqb4WL9QxrLZtFie+1SeooQJQn0QiyGM4XeMZhEUY928DoTw+j/alLrrdFhz+8A/UstWScd+OPL80/o6d5JBIgGWCMqWPWnnMu2LVdmwMZsoFhBNJLBN4GVqeqlLzaUi4yBpkAMujDFSDoa+eSgmcvmaYALfoMCyrrOvBfzTfHdX3vqXaNFlW8qGj1EjLWa+IwKlQz3u2I3vvGuAV3f3O2cKQWP05GgSPWmXMLGi6nF28cnJYlzRPBHq5sIZJXiBD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KMbvz3/qcmKYRW8+Ds7m8mMWaopcaeZ4V8Oqx9pYu6QxE1UKH14cMZecsAUL8zVxemA0iVBJ7L+4IzhrazUGYpUTe1jLJBElWNJXTMhuYYR71nEgk+cngOtaf7Iu+dMIS3CNN7GY1rwLzCOgiEzi13b1OLVHvuC6u3");



        }


        private void Button_Submit(object sender, RoutedEventArgs e)
        {
         
            if ("请选择文件" == selectFile.Content.ToString())
            {
                HandyControl.Controls.Growl.WarningGlobal("请选择文件!");

                return;
            }

            if (String.IsNullOrWhiteSpace(code.Text))
            {
                HandyControl.Controls.Growl.WarningGlobal("上传文件错误");

                return;
            }

            openFileDialog = new OpenFileDialog();
            (new MainWindow(this.examIneeList, this.groupData, this.jobList, this)).Show();

            this.Hide();

          

        }

        private void Button_SelectFile(object sender, RoutedEventArgs e)
        {
            fileHash = new Hashtable();
            openFileDialog = new OpenFileDialog
            {
                //openFileDialog.InitialDirectory = Config.keyPath;//注意这里写路径时要用c:\\而不是c:\
                Filter = "所有文件(*.zip)|*.zip",
                RestoreDirectory = true,
                FilterIndex = 1
            };

            if (openFileDialog.ShowDialog() != true)
            {
                return;
            }
            var zipName = openFileDialog.FileName;

            this.selectFile.Content = ""+System.IO.Path.GetFileName(openFileDialog.FileName);

            //把压缩包的里的文件解压到用户的文档目录
            using (FileStream zipFileToOpen = new FileStream(zipName, FileMode.Open))
            {
                try
                {
                    using (ZipArchive archive = new ZipArchive(zipFileToOpen, ZipArchiveMode.Read))
                    {
                        foreach (var zipArchiveEntry in archive.Entries)
                        {

                            ZipArchiveEntry entry = archive.GetEntry(zipArchiveEntry.FullName);
                            Console.WriteLine(entry.Name);
                            using (System.IO.Stream stream = entry.Open())
                            {
                                try
                                {
                                    System.IO.Stream output = new FileStream(Config.Document + entry.Name, FileMode.Create);

                                    fileHash.Add(entry.Name, Config.Document + entry.Name);

                                    int b = -1;
                                    while ((b = stream.ReadByte()) != -1)
                                    {
                                        output.WriteByte((byte)b);
                                    }
                                    output.Close();

                                }
                                catch (Exception)
                                {
                                    HandyControl.Controls.Growl.WarningGlobal("错误的密钥文件!");

                                }

                            }
                        }

                        //再去解密一个个 文件
                        foreach (String key in fileHash.Keys)
                        {

                            string content = "";
                            try
                            {
                                string jsonInfo = RSAHelper.ReadFile(fileHash[key].ToString());

                                content = RSAHelper.DecryptByPrivate(RSAUtils.PEMConvertToXML(), jsonInfo);

                                using (StreamWriter writer = new StreamWriter(Config.Document + key + ".json"))
                                {

                                    writer.WriteLine(Convert.ToString(content));
                                }
                            }
                            catch (Exception)
                            {
                                HandyControl.Controls.Growl.Warning("解密失败!");
                                return;
                            }



                            if ("examinee.data".Equals(key))
                            {
                                examIneeList= ExamIneeSort.DecodeHashExam(content);
                            }

                            if ("group.data".Equals(key))
                            {
                                JavaScriptSerializer Serializer = new JavaScriptSerializer();
                                groupData = Serializer.Deserialize<Group>(content);
                            }

                            if ("job.data".Equals(key))
                            {
                                JavaScriptSerializer Serializer = new JavaScriptSerializer();
                                jobList = Serializer.Deserialize<List<Job>>(content);

                            }


                        }
                        this.code.Text =""+ groupData.name;

                        //this.fileImg.Visibility = Visibility.Hidden;

                    }
                }
                catch (Exception)
                {
                    HandyControl.Controls.Growl.WarningGlobal("错误的密钥文件!");
                }
               
            }

        }



       
        private void Code_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {

            if (e.Key != System.Windows.Input.Key.Enter)
            {
                return;
            }

            this.Button_Submit(sender, e);
        }
    }
}
